package com.iplay.service.order.query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.iplay.entity.user.Role;
import com.iplay.entity.user.SimplifiedUserDO;
import com.iplay.service.query.QueryCondition;
import com.iplay.service.user.UserService;

@Component
public class UsernameQueryConditionBuilder{

	@Autowired
	private UserService userService;
	
	public QueryCondition create(Object t, String key) {
		SimplifiedUserDO findedUser = userService.findSimplifiedUserByUsername(String.class.cast(t))
				.filter(user -> user.getRole() != Role.ADMIN).orElse(new DummySimplifiedUserDO());
		return new QueryCondition(key, findedUser.getId());
	}

	private class DummySimplifiedUserDO implements SimplifiedUserDO {

		@Override
		public String getUsername() {
			return null;
		}

		@Override
		public Role getRole() {
			return null;
		}

		@Override
		public int getId() {
			return -1;
		}

	}
}
